import 'package:flutter/material.dart';
import 'package:sjx/repository/model/task_status_number_model.dart';

import 'item/task_receive_item_page.dart';

class ReceiveTaskPage extends StatefulWidget {
  final TaskStatusNumberModel taskStatusNumberModel;
  final int initialIndex;

  const ReceiveTaskPage({
    Key? key,
    required this.taskStatusNumberModel,
    this.initialIndex = 0,
  }) : super(key: key);

  @override
  _ReceiveTaskPageState createState() => _ReceiveTaskPageState();
}

class _ReceiveTaskPageState extends State<ReceiveTaskPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final List<ReceiveTaskItemPage> _tabPages = [];
  final List<ValueNotifier<int>> _countNotifiers = [];

  @override
  void initState() {
    super.initState();
    _initializeTabs();
  }

  void _initializeTabs() {
    _countNotifiers.addAll([
      ValueNotifier<int>(0),
      ValueNotifier<int>(widget.taskStatusNumberModel.myOrderReadyToSubmitOrderCount),
      ValueNotifier<int>(widget.taskStatusNumberModel.myOrderReadyToApproveCount),
      ValueNotifier<int>(widget.taskStatusNumberModel.myOrderRejectCount),
      ValueNotifier<int>(widget.taskStatusNumberModel.myOrderGotMoneyCount),
      ValueNotifier<int>(widget.taskStatusNumberModel.myOrderFailedCount),
    ]);

    _tabPages.addAll([
      ReceiveTaskItemPage(type: 0, countNotifier: _countNotifiers[0]),
      ReceiveTaskItemPage(type: 1, countNotifier: _countNotifiers[1]),
      ReceiveTaskItemPage(type: 2, countNotifier: _countNotifiers[2]),
      ReceiveTaskItemPage(type: 3, countNotifier: _countNotifiers[3]),
      ReceiveTaskItemPage(type: 4, countNotifier: _countNotifiers[4]),
      ReceiveTaskItemPage(type: 5, countNotifier: _countNotifiers[5]),
    ]);

    _tabController = TabController(
      length: _tabPages.length,
      vsync: this,
      initialIndex: widget.initialIndex,
    );
  }

  @override
  void dispose() {
    _tabController.dispose();
    for (var notifier in _countNotifiers) {
      notifier.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('领取的任务'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.pop(context),
        ),
        bottom: _buildTabBar(),
      ),
      body: _buildTabBarView(),
    );
  }

  PreferredSizeWidget _buildTabBar() {
    return TabBar(
      controller: _tabController,
      isScrollable: true,
      labelColor: Theme.of(context).primaryColor,
      unselectedLabelColor: Colors.grey,
      labelStyle: const TextStyle(
        fontSize: 14,
        fontWeight: FontWeight.bold,
      ),
      unselectedLabelStyle: const TextStyle(
        fontSize: 14,
        fontWeight: FontWeight.normal,
      ),
      indicator: const UnderlineTabIndicator(
        borderSide: BorderSide(
          width: 4,
          color: Color.fromRGBO(255, 75, 85, 1), // 使用主题色
        ),
        insets: EdgeInsets.only(bottom: 1),
      ),
      tabs: [
        const Tab(text: '全部'),
        ValueListenableBuilder<int>(
          valueListenable: _countNotifiers[1],
          builder: (context, count, child) {
            return Tab(text: '待提交($count)');
          },
        ),
        ValueListenableBuilder<int>(
          valueListenable: _countNotifiers[2],
          builder: (context, count, child) {
            return Tab(text: '待审核($count)');
          },
        ),
        ValueListenableBuilder<int>(
          valueListenable: _countNotifiers[3],
          builder: (context, count, child) {
            return Tab(text: '已拒绝($count)');
          },
        ),
        ValueListenableBuilder<int>(
          valueListenable: _countNotifiers[4],
          builder: (context, count, child) {
            return Tab(text: '已获赏($count)');
          },
        ),
        ValueListenableBuilder<int>(
          valueListenable: _countNotifiers[5],
          builder: (context, count, child) {
            return Tab(text: '任务失败($count)');
          },
        ),
      ],
    );
  }

  Widget _buildTabBarView() {
    return TabBarView(
      controller: _tabController,
      children: _tabPages,
    );
  }
}